package org.pp.controller;


import cn.hutool.core.util.IdUtil;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.pp.config.MiniappConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay/native")
@Slf4j
public class PayNativeController {


    @Autowired
    MiniappConfig miniappConfig;

    @Resource
    RSAAutoCertificateConfig config;

    @GetMapping("/prepay")
    @Operation(summary = "测试支付二维码预支付")
    public void prepay(){
        // 构建service
        NativePayService service = new NativePayService.Builder().config(config).build();
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(1);
        request.setAmount(amount);
        request.setAppid(miniappConfig.getAppid());
        request.setMchid(miniappConfig.getMerchantId());
        request.setDescription("商品:测试");
        request.setNotifyUrl(miniappConfig.getNotify());
        request.setOutTradeNo(IdUtil.fastSimpleUUID());
        // 调用下单方法，得到应答
        PrepayResponse response = service.prepay(request);
        // 使用微信扫描 code_url 对应的二维码，即可体验Native支付
        System.out.println(response.getCodeUrl());

        //return BeanUtil.copyProperties(prepayWithRequestPaymentResponse, PayResponse.class);
    }


}
